package com.hgd.security.evp.biz.biz;

import com.hgd.security.evp.biz.entity.EvpMenu;
import com.hgd.security.evp.biz.entity.EvpUser;
import com.hgd.security.evp.biz.mapper.EvpMenuMapper;
import com.hgd.security.evp.biz.vo.EMenuVo;
import com.hgd.security.evp.biz.vo.MenuTree;
import com.hgd.security.evp.common.biz.BusinessBiz;
import com.hgd.security.evp.common.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author By Shang
 * @email ${email}
 * @version 2019-01-01 22:49:10
 */
@Service
public class EvpMenuBiz extends BusinessBiz<EvpMenuMapper, EvpMenu> {

	@Autowired
	private Environment environment;

	@Autowired
	private EvpUserBiz eUserBiz;

    /**
     * 添加单条记录
     * @param eMenu 待添加记录
     * @return 结果
     */
    public ObjectResultResponse<Void> addEmenu(EvpMenu eMenu) {
        ObjectResultResponse<Void> oResult=new ObjectResultResponse<>();

		check(eMenu, oResult, false);
		if (oResult.getStatus() == 400) {
			return oResult;
		}

        this.insertSelectiveBindCrt(eMenu);
        oResult.setMessage("添加菜单成功");
        return oResult;
    }

	private void check(EvpMenu menuToOpe, ObjectResultResponse<Void> oResult, boolean isUpdate) {
		/*
		 * 1 编辑不能重复
		 * 2 名称不能重复
		 */
		Example example = new Example(EvpMenu.class).selectProperties("id", "evpCode", "evpTitle");
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("isDeleted", BooleanUtil.BOOLEAN_FALSE);
		criteria.andEqualTo("evpCode", menuToOpe.getEvpCode());

		Example.Criteria criteria1 = example.or();
		criteria1.andEqualTo("isDeleted", BooleanUtil.BOOLEAN_FALSE);
		criteria1.andEqualTo("evpTitle", menuToOpe.getEvpTitle());

		List<EvpMenu> eMenus = this.selectByExample(example);

		if (EmptyUtil.isNotEmpty(eMenus)) {
			if (isUpdate) {
				eMenus.forEach(menu -> {
					if (StringUtil.equals(menu.getEvpCode(), menuToOpe.getEvpCode()) && !menu.getId()
						.equals(menuToOpe.getId())) {
						oResult.setStatus(400);
						oResult.setMessage("菜单编号已存在");
						return;
					}
					if (StringUtil.equals(menu.getEvpTitle(), menuToOpe.getEvpTitle()) && !menu.getId()
						.equals(menuToOpe.getId())) {
						oResult.setStatus(400);
						oResult.setMessage("菜单名称已存在");
						return;
					}
				});
			} else {
				eMenus.forEach(menu -> {
					if (StringUtil.equals(menu.getEvpCode(), menuToOpe.getEvpCode())) {
						oResult.setStatus(400);
						oResult.setMessage("菜单编号已存在");
						return;
					}
					if (StringUtil.equals(menu.getEvpTitle(), menuToOpe.getEvpTitle())) {
						oResult.setStatus(400);
						oResult.setMessage("菜单名称已存在");
						return;
					}
				});
			}
		}
	}

	/**
     * 获取菜单树
     * @return 菜单树
     */
    public List<MenuTree> getMenuTree() {
        List<EvpMenu> allEmenu = getAllEmenu();

        List<MenuTree> menuTrees=new ArrayList<>();
		if (EmptyUtil.isNotEmpty(allEmenu)) {
			allEmenu.forEach(menu -> {
				MenuTree menuTree = new MenuTree(menu.getId(), menu.getParentId(), menu.getEvpTitle());
				menuTree.setHref(menu.getEvpHref());
				if (StringUtil.isBlank(menu.getEvpHref())) {
					menuTree.setState(environment.getProperty("treeMenuClosedStatus"));
				}
				menuTrees.add(menuTree);
			});
		}

        return TreeUtil.buildTree(menuTrees, -1);
    }

    /**
     * 获取全部在使用部门
     * @return 全部在使用部门
     */
    public List<EvpMenu> getAllEmenu(){
		String str="";

        Example example=new Example(EvpMenu.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("enabled", BooleanUtil.BOOLEAN_FALSE);
		criteria.andEqualTo("isDeleted", BooleanUtil.BOOLEAN_FALSE);

		example.setOrderByClause("order_num asc");
        List<EvpMenu> eMenus = this.selectByExample(example);
        if(EmptyUtil.isEmpty(eMenus)){
            return new ArrayList<>();
        }

        return eMenus;
    }

	public List<MenuTree> getAuthorityMenu(Integer userId) {
		// 1->获取为当前人员分配的菜单，获取依据为当前人员所在的组
		EvpUser eUser = eUserBiz.selectById(userId);
		List<EvpMenu> eMenuList = null;
		if (StringUtil.equals(eUser.getIsSuper(), BooleanUtil.BOOLEAN_TRUE)) {
			eMenuList = getAllEmenu();
		} else {
			eMenuList = this.mapper.getAuthorityMenu(userId);
		}

		if(EmptyUtil.isEmpty(eMenuList)) {
			return null;
		}

		List<MenuTree> menuTrees = new ArrayList<>();
		if (EmptyUtil.isNotEmpty(eMenuList)) {
			eMenuList.forEach(menu -> {
				MenuTree menuTree = new MenuTree(menu.getId(), menu.getParentId(), menu.getEvpTitle());
				menuTree.setHref(menu.getEvpHref());
				if (StringUtil.isBlank(menu.getEvpHref())) {
					menuTree.setState(environment.getProperty("treeMenuClosedStatus"));
				}
				menuTrees.add(menuTree);
			});
		}

		return TreeUtil.buildTree(menuTrees, -1);
	}

	public ObjectResultResponse<EMenuVo> getMenuListById(Integer id) {
		EMenuVo vo = this.mapper.getByIdBindParent(id);
		if (StringUtil.isBlank(vo.getParentName())) {
			vo.setParentName("");
		}
		return new ObjectResultResponse<>(vo);
	}

	public ObjectResultResponse<Void> updateEmenu(EvpMenu eMenu) {
		ObjectResultResponse<Void> restResult = new ObjectResultResponse<>();

		check(eMenu, restResult, true);
		if (restResult.getStatus() == 400) {
			return restResult;
		}
		this.updateSelectiveByIdBindUpd(eMenu);

		restResult.setMessage("编辑菜单成功");
		restResult.setStatus(200);
		return restResult;
	}

	public ObjectResultResponse<Void> delMenuListById(Integer id) {
		ObjectResultResponse<Void> restResult = new ObjectResultResponse<>();

		EvpMenu eMenu = new EvpMenu();
		eMenu.setId(id);
		eMenu.setIsDeleted(BooleanUtil.BOOLEAN_TRUE);
		this.updateSelectiveByIdBindUpd(eMenu);

		restResult.setMessage("删除菜单成功");
		restResult.setStatus(200);
		return restResult;
	}

	/**
	 * 按ID查询菜单集合  "1,2,3,4"
	 */
	public List<EvpMenu> getListByIds(String ids) {
		return this.mapper.selectByIds(ids);
	}

	/**
	 * 获取某组下的菜单
	 *
	 * @param groupId
	 * @return
	 */
	public List<EvpMenu> getMenuOfGroup(Integer groupId) {
		List<EvpMenu> eMenuList = this.mapper.getMenuOfGroup(groupId);
		if (EmptyUtil.isNotEmpty(eMenuList)) {
			return eMenuList;
		}

		return new ArrayList<>();
	}
}